<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="css/Default.css"/>
<Script language="javascript" src="data/password.js"></script>
<Script language="javascript" src="exec/login.js"></script>
<noscript>
location.href="noscript.html";
</noscript>
<SCRIPT LANGUAGE="JavaScript">
	window.onload = redir(1);
//name of cookie: 'eomdmf'

</SCRIPT>

</head>

<body>
<?php
//import form information

$feed_data = $_POST['Announcements']; //feed_data is one long string with all the newsfeeds
$feed_data = stripslashes($feed_data);
$pieces = explode("\n", $feed_data); //breaks up the feed_data string into an array of the newsfeeds
$feed_data = "";//free up space
$today = getdate(); //gets the current date
$todayvalue = (($today["mon"]+3)%12)*100 + $today["mday"]; //calculates the 4-digit value of the date (Oct 14 = 114, Jan 10 = 410)
		
		
for($i = 0; $i < sizeof($pieces); $i++)
{
	$newstr = substr($pieces[$i], 0, strpos($pieces[$i], ' ')); //Gets the month name from each newsfeed in the array
	$tmp = getMonthValue($newstr); //Gets the value of the month (Oct = 1, Feb = 5)
	$tmp = (($tmp + 3) % 12) * 100; //Multiplies the month vlaue by 100 so it can be added to the day value
	$tmp2 = (substr($pieces[$i], strpos($pieces[$i], (' ')) + 1, strpos($pieces[$i], (':')) - 2)-0); //Gets the day of the month from the newsfeed
	$itemdate = $tmp + $tmp2; //Adds the month and day value to calculate the 4-digit date value

	if($itemdate >= $todayvalue) //Compares the current 4-digit date value, and newsfeed 4-digit date value to check if the newsfeed is in the past or not
	{		
		$feed_data = $feed_data."\n". str_replace("\n","",$pieces[$i]); //Adds the newsfeed to feed_data if it's not in the past
	}
}
//echo data, so that if we need to we can see it for debugging.
echo "<!--".$feed_data."-->";

?>

<table height="100%" width="100%" border="0">
<tr>
<td>
<center>

			<p style="font-family: Arial; font-size:20px">Your events have been saved.</p>
			<FORM METHOD="LINK" ACTION="index.html">
			<INPUT TYPE="submit" VALUE="Home Page">
			</FORM>

			<FORM METHOD="LINK" ACTION="LoggedIn.php">
			<INPUT TYPE="submit" VALUE="Return to Main Menu">
			</FORM>

</center>
</td>
</tr>
</table>
<?php

//write the data to the newsfeed buffer file.
$fh = fopen("data/newsfeed.txt", "w+");

if($fh==false)
	die("unable to create file");

$i=0;
fwrite($fh,$feed_data);
fclose($fh);

//open the buffer file so that we can parse it into an array.
$fh = fopen("data/newsfeed.txt", "r");
$i=0;
$feed_data = ""; 

while(!feof($fh))
{
	$feed_data = $feed_data . trim(fgets($fh))."\n"; //Recreates feed_data to make it parse correctly
	$i++;
}
fclose($fh);

$item = explode("\n", $feed_data); //Breaks up the $feed_data string again

//write to the newsfeed java script file
$fh = fopen("data/newsfeed_data.js", "w+");
if($fh==false)
	die("unable to create file");
fwrite($fh, "var items = new Array();\n");

$i=0;
while($i <= sizeof($item) )
{
	fwrite($fh, "items[$i] = \"".trim($item[$i])."\";\n");
	$i++;
}

fclose($fh);


function getMonthValue($str) //Calculates the month value of each month
{
	$months_array = array(0 => 'Jan', 1 => 'Feb', 2 =>'March', 3 =>'April', 4 =>'May', 5 =>'June', 6 =>'July', 7 =>'Aug', 8 =>'Sept', 9 =>'Oct', 10 =>'Nov', 11 =>'Dec');
		
		for($i = 0; $i < sizeof($months_array); $i++)
		{
			if($str == $months_array[$i])
			{
			return $i + 1;
			}
		}
}?>

</body>
</html>
